package me.aurora.app.rest.system;

import me.aurora.domain.Menu;
import me.aurora.domain.User;
import me.aurora.domain.vo.MenuVo;
import me.aurora.service.MenuService;
import me.aurora.service.UserService;
import me.aurora.service.dto.MenuDTO;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 郑杰
 * @date 2018/08/23 17:27:57
 */
@RestController
@RequestMapping("menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private UserService userService;

    /**
     * 构建前端路由
     */
    @GetMapping(value = "/buildMenuUrl")
    public Object buildMenuUrl(HttpServletRequest request){
        //查询出所有子菜单
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        user = userService.findById(user.getId());
        List<MenuDTO> menuDTOS = menuService.findMenusByUserRols(user.getRoles());
        List<MenuVo> menuVoList = menuService.buildMenuUrl(menuDTOS);
        return menuVoList;
    }
}
